package com.palfund.nested;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by clvc on 2017/10/31.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 */
//RecyclerView嵌套RecyclerView外层adapter
public class RRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<ArrayList<String>> mList = new ArrayList<>();
    private Context mContext;

    public RRAdapter(Context context) {
        mContext = context;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("RRAdapter", "---onCreateViewHolder()--->" + "outCreate");
        return new RRViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recyclerviewtop,
                parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.i("RRAdapter", "---onBindViewHolder()--->" + "OutBind   ---   " + position);
        RRViewHolder rrViewHolder = (RRViewHolder) holder;
        RVAdapter rvAdapter = (RVAdapter) rrViewHolder.mRecyclerView.getAdapter();
        rvAdapter.addAll(mList.get(position), true);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class RRViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView mRecyclerView;

        public RRViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = itemView.findViewById(R.id.recyclerViewTop);
            GridLayoutManager manager = new GridLayoutManager(mContext, 2, GridLayoutManager
                    .HORIZONTAL, false);
            mRecyclerView.setLayoutManager(manager);
            RVAdapter rvAdapter = new RVAdapter(mContext);
            rvAdapter.orientation=1;
            mRecyclerView.setAdapter(rvAdapter);
        }
    }

    public void addAll(ArrayList<ArrayList<String>> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }
}
